﻿using System.Collections.Generic;
using System.Windows.Forms;
using yafc.Activity;
using yafc.Interface;
using yafc.Interface.Api;
using System.Net;
using System;
using System.Drawing;

namespace yafc.UI.Components
{
	public partial class SessionView : UserControl
	{
		private Session session;
		private ListBox list = new ListBox();
		private int max = 20;

		private bool sending = false;

		public int MaxPost
		{
			get { return max; }
			set { max = value; }
		}

		public SessionView(string name)
		{
			InitializeComponent();

			session = new Session(name);
			session.NewPost += OnNewPost;
			session.LoginResponse += OnLogin;
			session.SendResponse += OnSend;

			layout.Controls.Add(list, 0, 1);
			layout.SetColumnSpan(list, 2);
			list.Dock = DockStyle.Fill;

			list.Comparison = PostItemCompare;
		}

		public void Login(IPlugin plugin)
		{
			LoginBox.Instance().ShowDialog(plugin.Name, 
				(Name, Passwd) => Login(plugin.Factory, plugin.Name, Name, Passwd),
				LoginValidator);
		}
		public void Send(string Text)
		{
			session.Send(Text);
		}

		protected virtual void Login(IFactory f, string Serv, string Uname, string Passwd)
		{
			if (session.Contains(Serv, Uname))
			{
				session.Relogin(Serv, Uname, Passwd);
			}
			else
			{
				session.Login(f, Serv, Uname, Passwd);
			}
		}
		protected virtual bool LoginValidator(string Serv, string Uname, out string Reason)
		{
			Reason = null;
			if (session.Contains(Serv, Uname))
			{
				Reason = "当前会话中已登录了" + Uname + "@" + Serv;
				return false;
			}
			return true;
		}

		protected virtual void OnNewPost(object sender, NewPostEventArgs e)
		{
			MethodInvoker operation;
			operation = delegate
			{
				foreach (var p in e.posts)
				{
					list.Add(new PostItem(p));
				}
				if (e.posts.Count > 0)
				{
					list.Invalidate();
				}
			};
			this.BeginInvoke(operation);
		}
		protected virtual void OnLogin(object sender, LoginResults lr)
		{
			MethodInvoker operation;
			switch (lr.status)
			{
			case HttpStatusCode.OK:
				operation = delegate
				{
					LoginBox.Instance().Hide();
				};
				break;

			case HttpStatusCode.Unauthorized:
				operation = delegate
				{
					LoginBox.Instance().LoginFailed("用户名或密码错误");
				};
				break;

			default:
				operation = delegate
				{
					LoginBox.Instance().LoginFailed("网络错误，请重试。");
				};
				break;
			}
			LoginBox.Instance().BeginInvoke(operation);
		}
		protected virtual void OnSend(object sender, SendResults sr)
		{
			switch (sr.status)
			{
			case HttpStatusCode.OK:
				if (sending)
				{
					BeginInvoke((MethodInvoker)(() => inputBox.Clear()));
				}
				break;
			default:
				throw new Exception("Unhandled error in OnSend!");
			}
		}

		protected int PostItemCompare(ListBoxItem a, ListBoxItem b)
		{
			var lhs = a as PostItem;
			var rhs = b as PostItem;
			if (lhs.Post.timestamp == rhs.Post.timestamp)
				return 0;
			else if (lhs.Post.timestamp < rhs.Post.timestamp)
				return 1;
			else
				return -1;
		}

		protected void DrawFloatToolbar(Graphics g, int X, int Y)
		{

		}

		private void sendBtn_Click(object sender, EventArgs e)
		{
			if (inputBox.Text.Length > 0)
			{
				Send(inputBox.Text);
				sending = true;
			}
		}

		private void inputBox_Enter(object sender, EventArgs e)
		{
			inputBox.SelectAll();
		}
		private void inputBox_TextChanged(object sender, EventArgs e)
		{
			sending = false;
		}
	}
}
